<?php
namespace app\service;

use think\facade\Db;

class ArticleService
{
    public static function datalist($param=null)
    {
        $where=array();
        if(!empty($param['where']))
        {
            $where=$param['where'];
        }
        $order='id desc';
        if(!empty($param['order']))
        {
            $order=$param['order'];
        }
        $field='*';
        if(!empty($param['field']))
        {
            $field=$param['field'];
        }
        $datalist=Db::name('article')->where($where)->field($field)->order($order)->paginate(['list_rows'=>10,'query'=>$param['query']]);
        $data_list=array();
        foreach($datalist as $k=>$v)
        {
            $data_list[]=self::handleData($v);
        }
        return ['datalist'=>$data_list,'total'=>$datalist->total(),'pager'=>$datalist->render()];
    }
    public static function info($param)
    {
        $data=Db::name('article')->where($param['where'])->find();
        $data=self::handleData($data);
        return $data;
    }
    public static function handleData($v)
    {
        if($v)
        {
        $v['add_time']=formatdate($v['add_time']);
        $v['category']=Db::name('category')->where('id',$v['category_id'])->find();
        $v['category_name']=empty($v['category'])?'':$v['category']['name'];
        if(isset($v['images']))
        {
            $v['images_v'] = $v['images'];
            $v['images'] = handleImg($v['images']);
            $v['image'] = empty($v['images'])?'':$v['images'][0];
        }
        if(isset($v['contents']))
        {
            $v['mobile_content']=strip_tags($v['contents']);
        }
        }
        return $v;
    }
}